package com.jic.manager.impl;

import java.lang.Long;
import java.util.Date;
import java.util.List;

import com.jic.common.base.vo.RestResult;
import com.jic.manager.entity.Channel;
import com.jic.manager.mapper.ChannelMapper;
import com.jic.manager.request.ChannelRequest;
import com.jic.manager.service.ChannelService;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;

import com.jic.util.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 渠道表
 * @author : 
 * @email : 
 * @since : 2020-02-26 09:41:13
 * @version : v1.0.0
 */
@Service
@Slf4j
public class ChannelServiceImpl implements ChannelService{

    @Resource
    private ChannelMapper channelMapper;

    @Override
    public long insert(ChannelRequest request) {
        Channel channel = new Channel();
        BeanUtils.copyProperties(request,channel);
        long flag = channelMapper.insert(channel);
        return flag;
    }

    @Override
    public long delete(ChannelRequest channelRequest) {
        Channel channel = new Channel();
        BeanUtils.copyProperties(channelRequest,channel);
        return channelMapper.delete(channel);
    }

    @Override
    public long update(ChannelRequest request) {
        Channel channel = new Channel();
        BeanUtils.copyProperties(request,channel);
        return channelMapper.update(channel);
    }

    @Override
    public Channel queryByPrimaryKey(Long id){
        return channelMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<Channel> query(Channel channel){
        return channelMapper.query(channel);
    }

    @Override
    public PageResult<Channel> queryPage(ChannelRequest channelRequest) {
        //计算下标
        int startIndex = (channelRequest.getStart() - 1) * channelRequest.getLimit();
        channelRequest.setDeleteFlag(0);
        List<Channel> list = channelMapper.queryPage(startIndex, channelRequest.getLimit(), channelRequest);
        channelMapper.queryPageCount(channelRequest);
        PageResult<Channel> pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(list.size());
        return pageResult;
    }

    @Override
    public RestResult editMerchantInfo(ChannelRequest channelRequest) {
        try{
            Channel channel = new Channel();
            BeanUtils.copyProperties(channelRequest,channel);
            channel.setDeptId(channelRequest.getAdminOperate() != null ? channelRequest.getAdminOperate().getDeptId() :null);
            Long id = channelRequest.getId();
            if (id != null && id !=0){
                channelMapper.update(channel);
            }else {
                //设置是否删除
                channelRequest.setDeleteFlag(0);
                channelMapper.insert(channel);
            }
            return RestResult.success("成功");
        }catch (Exception e){
            log.error("error",e);
           return RestResult.error("9999",e.getLocalizedMessage());
        }

    }
}